﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Model;
using AvatarHelper;
using Avatar.MembershipService;

public partial class Company_Transactions : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.bind(0);
        }
    }
    protected void btn_Up_Click(object sender, EventArgs e)
    {
        int _page;
        _page = Convert.ToInt32(lbl_Current.Text) - 1;
        if (_page > 0)
        {
            _page = _page - 1;
        }
        this.bind(_page);
    }
    protected void btn_Down_Click(object sender, EventArgs e)
    {
        int _page;
        _page = Convert.ToInt32(lbl_Current.Text) - 1;
        if (_page < Convert.ToInt32(lbl_Total.Text) - 1)
        {
            _page = _page + 1;
        }
        this.bind(_page);
    }
    protected void btn_Goto_Click(object sender, EventArgs e)
    {
        int _page;
        try
        {
            _page = Convert.ToInt32(txt_Page.Text) - 1;
            if (_page > Convert.ToInt32(lbl_Total.Text) - 1) throw new Exception("too big");
            if (_page < 0) throw new Exception("too small");
        }
        catch (Exception)
        {
            _page = Convert.ToInt32(lbl_Current.Text) - 1;
        }
        this.bind(_page);
    }
    protected void bind(int _page)
    {
        int _record = 15;
        var _user = LoginManager.GetCurrentUser();
        if (_user != null)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                lbl_Current.Text = (_page + 1).ToString();
                lbl_Total.Text = (_adc.CompanyFinancialAccountLog.Count(l => l.Company == _user.UserName) / _record + 1).ToString();
                var _log = _adc.CompanyFinancialAccountLog.Where(l => l.Company == _user.UserName).OrderByDescending(l => l.Date).Skip(_record * _page).Take(_record);
                ListView1.DataSource = _log;
                ListView1.DataBind();
            }
        }
    }
}
